package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.*;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service//EmployeeServiceImpl放入容器中
public class DishServiceImpl implements DishService{
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Override
    public Page findPage(Integer pageNum, Integer pageSize , String name) {

        //设置查询菜品的条件
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        //根据name进行模糊查询
        //select * from dish where name like %#{name}%
        wrapper.like(StringUtils.isNoneEmpty(name),Dish::getName,name);
        //根据传递的pageNum和pageSize创建一个Page<Dish>
        Page<Dish> page = new Page<>(pageNum,pageSize);
        //获取page对象
        page = dishMapper.selectPage(page,wrapper);
        //获取page对象中的菜品
        List<Dish> dishList = page.getRecords();
        //进行非空判断后，进行遍历。朝招相应的DishFlavor和category
        if (CollectionUtil.isNotEmpty(dishList)){
            //遍历
            for (Dish dish : dishList) {
                //dish中的categoryId
                //通过categoryMapper获取到对应的category
                Category category = categoryMapper.selectById(dish.getCategoryId());
                //将dish中的CategoryName属性设置成获取到的分类
                dish.setCategoryName(category.getName());

                //获取每个菜品的口味DishFlavor
                //编写条件
                //select * from dish_flavor where dish_id = dish.getId ;
                LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
                wrapper1.eq(DishFlavor::getDishId,dish.getId());
                //调用selectList进行查询
                List<DishFlavor> flavorList = dishFlavorMapper.selectList(wrapper1);
                //然后将得到的DishFlavor设置到dish对象中去
                dish.setFlavors(flavorList);
            }
        }
        //返回page对象
        return page;
    }


    @Transactional
    @Override
    public void saveDish(Dish dish) {
        //保存菜对象
        dishMapper.insert(dish);

        //保存口味
        //insert into dish_flavor values()
        List<DishFlavor> flavorList = dish.getFlavors();

        if (CollectionUtil.isNotEmpty(flavorList)) {
            for (DishFlavor dishFlavor : flavorList) {
                //设置口味对象的dish_id
                dishFlavor.setDishId(dish.getId());
                //插入dishFlavorMapper数据
                dishFlavorMapper.insert(dishFlavor);
            }
        }
    }
    @Transactional
    @Override
    public void editDish(Dish dish) {
        //根据主键进行更新菜品
        dishMapper.updateById(dish);
        //要根据获取的dish的id删除dish_flavor中的相关数据
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId,dish.getId());
        //调用方法删除
        dishFlavorMapper.delete(wrapper);
        //在获取新更改的口味然后保存到dish_flavor
        List<DishFlavor> flavorList = dish.getFlavors();
        if (CollectionUtil.isNotEmpty(flavorList)) {
            for (DishFlavor dishFlavor : flavorList) {
                dishFlavor.setDishId(dish.getId());
                dishFlavorMapper.insert(dishFlavor);
            }
        }
    }

    @Transactional
    @Override
    public void delete(Long[] ids) {
        //首先确定删除的套餐中有没有未停售的
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Dish::getId,ids);
        wrapper.eq(Dish::getStatus,1);
        Integer count = dishMapper.selectCount(wrapper);
        if (count > 0){
            throw new CustomException("对不起你选择菜品，还存在正在销售的无法删除");
        }
        //遍历ids
        if (ids != null && ids.length != 0){
            for (Long dishId : ids) {
                //先删除其口味
                LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
                wrapper1.eq(DishFlavor::getDishId,dishId);
                dishFlavorMapper.delete(wrapper1);
                //再删除其菜品
                dishMapper.deleteById(dishId);
            }
        }
    }

    @Override
    public void statusChange(Integer status, Long[] ids ) {
        //编写更改语句
        //update dish set status = {status} where ids in ();
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();

        //遍历数组然后进行更改
        if (ids != null && ids.length != 0){
            for (Long dishId : ids) {
                Dish dish = new Dish();
                dish.setStatus(status);
                dish.setId(dishId);
                dishMapper.updateById(dish);
            }
        }
    }


    @Override
    public Dish queryDishById(Long id) {
        //先获取菜品
        Dish dish = dishMapper.selectById(id);
        //根据获取的dish获取相应的口味和分类
        LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(DishFlavor::getDishId,dish.getId());
        //调用selectList进行查询
        List<DishFlavor> flavorList = dishFlavorMapper.selectList(wrapper1);
        //然后将得到的DishFlavor设置到dish对象中去
        dish.setFlavors(flavorList);
        return dish;
    }

    @Override
    public List<Dish> findDishList(String name,Long categoryId ,Integer status) {
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(name),Dish::getName,name);
        wrapper.eq(categoryId!=null,Dish::getCategoryId,categoryId);
        wrapper.eq(Dish::getStatus,status);
        List<Dish> dishList = dishMapper.selectList(wrapper);

        //获取口味
        if (CollectionUtil.isNotEmpty(dishList)){
            for (Dish dish : dishList) {
                //获取查找条件
                LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
                wrapper1.eq(DishFlavor::getDishId,dish.getId());
                List<DishFlavor> dishFlavors = dishFlavorMapper.selectList(wrapper1);
                dish.setFlavors(dishFlavors);
            }
        }
        /*if (name == null){
            LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Dish::getCategoryId,categoryId);
            dishList = dishMapper.selectList(wrapper);
        }else{
            LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
            wrapper.like(StringUtils.isNotEmpty(name),Dish::getName,name);
            dishList = dishMapper.selectList(wrapper);
        }*/
        return dishList;
    }
}
